// var express  = require('express');
// var router = express.Router();

var noteCateModel = require('../models/noteCateModel')
var NoteArticleModel = require('../models/noteArticleModel')
var NoteItemModel = require('../models/noteItemModel')

var async = require('async')

var noteCtrl = {};

noteCtrl.getCate = function (req, res) {


    noteCateModel.find({}). /*sort({ 'ctime': -1 })*/ /* .limit(10) */
        exec(function (err, items) {

            if (err) {

                res.status(500).json({
                    code: -1,
                    msg: '没有对应的信息'
                })
            } else {

                res.json({
                    code: 0,
                    dat: items
                })
            }

        });


};

noteCtrl.addCate = function (req, res, next) {

    // var sname = req.params.sname;

    var data = req.body
    console.log(data);
    // var newCate = new noteCateModel(data);

    noteCateModel.create(data, function (err, item) {

        if (err) {

            console.log(err)
            res.status(500).json({
                code: -1,
                msg: '保存数据错误'
            })
        } else {
            res.json({
                code: 0,
                dat: item
            })
        }

    });

}
noteCtrl.updateCate = function (req, res, next) {

    // var sname = req.params.sname;

    var data = req.body
    var _id = data._id
    // delete data._id;
    console.log(data);
    // var newCate = new noteCateModel(data);

    // console.log(newCate)

    noteCateModel.findByIdAndUpdate(_id, data, {
        new: true
    }, function (err, item) {

        if (err) {

            console.log(err)

            res.status(500).json({
                code: -1,
                msg: '保存数据错误'
            })
        } else {
            res.json({
                code: 0,
                dat: item
            })
        }

    });

}
noteCtrl.removeCate = function (req, res, next) {

    // var sname = req.params.sname;

    // var data = req.body
    // var _id = data._id
    // delete data._id;
    // console.log(data);
    // var newCate = new noteCateModel(data);

    // console.log(newCate)

    var _id = req.query._id;
    noteCateModel.findByIdAndRemove(_id, function (err, item) {

        if (err) {

            console.log(err)

            res.status(500).json({
                code: -1,
                msg: '保存数据错误'
            })
        } else {
            res.json({
                code: 0,
                dat: item
            })
        }

    });

}
noteCtrl.getItem = function (req, res) {

    var cateId = req.query.cateId;
    var sname = req.query.sname;
    var page = req.query.page || 1;
    var queryCondition = {

    };

    if (cateId) {
        queryCondition.cateId = cateId;
    }
    if (sname) {
        queryCondition.sname = {
            $regex: new RegExp(sname, "i")
        }
    }



    async.parallel({

        //
        count: function (done) {  // 查询数量
            NoteItemModel.count(queryCondition).exec(function (err, count) {
                done(err, count);
            });
        },

        //
        records: function (done) {   // 查询一页的记录
            NoteItemModel.find(queryCondition).sort({
                'ctime': -1
            }).skip((page - 1) * 10).limit(10).exec(function (err, docs) {
                done(err, docs);
            });
        },

    }, function (err, results) {

        var count = results.count;
        var records = results.records;
        if (err) {

            res.status(500).json({
                code: -1,
                msg: '没有对应的信息'
            })
        } else {

            res.json({
                code: 0,
                dat: {
                    total: count,
                    records: records
                }
            })
        }

    });

    // console.log(queryCondition)
    // NoteItemModel.find(queryCondition).sort({
    //     'ctime': -1
    // }).limit(10).
    // exec(function (err, items) {

    //     if (err) {

    //         res.status(500).json({
    //             code: -1,
    //             msg: '没有对应的信息'
    //         })
    //     } else {

    //         res.json({
    //             code: 0,
    //             dat: items
    //         })
    //     }

    // });


};

noteCtrl.addItem = function (req, res, next) {

    // var sname = req.params.sname;

    var data = req.body
    console.log(data);
    // var newCate = new noteCateModel(data);

    NoteItemModel.create(data, function (err, item) {

        if (err) {

            console.log(err)
            res.status(500).json({
                code: -1,
                msg: '保存数据错误'
            })
        } else {
            res.json({
                code: 0,
                dat: item
            })
        }

    });

}
noteCtrl.updateItem = function (req, res, next) {

    // var sname = req.params.sname;

    var data = req.body
    var _id = data._id
    // delete data._id;
    console.log(data);
    // var newCate = new noteCateModel(data);

    // console.log(newCate)

    NoteItemModel.findByIdAndUpdate(_id, data, {
        new: true
    }, function (err, item) {

        if (err) {

            console.log(err)

            res.status(500).json({
                code: -1,
                msg: '保存数据错误'
            })
        } else {
            res.json({
                code: 0,
                dat: item
            })
        }

    });

}
noteCtrl.removeItem = function (req, res, next) {

    // var sname = req.params.sname;

    var data = req.body
    var _id = data._id
    // delete data._id;
    console.log(data);
    // var newCate = new noteCateModel(data);

    // console.log(newCate)

    NoteItemModel.findByIdAndUpdate(_id, data, {
        new: true
    }, function (err, item) {

        if (err) {

            console.log(err)

            res.status(500).json({
                code: -1,
                msg: '保存数据错误'
            })
        } else {
            res.json({
                code: 0,
                dat: item
            })
        }

    });

}


noteCtrl.getArticle = function (req, res, next) {
    var _itemId = req.query.itemId;
    var _id = req.query._id;

    var queryCondition = {};

    if (_itemId) {
        queryCondition.itemId = _itemId;
    }
    if (_id) {
        queryCondition._id = _id;
    }

    NoteArticleModel.find(queryCondition).sort({
        'ctime': 1
    }). /* .limit(10) */ select({
        'ctime': 1,
        "sname": 1,
        "_id": 1
    }).
        exec(function (err, items) {

            if (err) {

                console.log(err);
                res.status(500).json({
                    code: -1,
                    msg: '没有对应的信息'
                })
            } else {

                res.json({
                    code: 0,
                    dat: items
                })
            }

        });
}

// getOne
noteCtrl.getOneArticle = function (req, res, next) {
    var _id = req.params.id;
    console.log(_id);

    NoteArticleModel.findOne({
        _id: _id
    }, function (err, item) {

        if (err) {

            res.status(500).json({
                code: -1,
                msg: '没有对应的信息'
            })
        } else {

            res.json({
                code: 0,
                dat: item
            })
        }

    });
}

//addArticle
noteCtrl.addArticle = function (req, res, next) {

    // var sname = req.params.sname;

    var data = req.body
    console.log(data);
    // var newCate = new noteCateModel(data);

    NoteArticleModel.create(data, function (err, item) {

        if (err) {

            console.log(err)
            res.status(500).json({
                code: -1,
                msg: '保存数据错误'
            })
        } else {
            res.json({
                code: 0,
                dat: item
            })
        }

    });

}

module.exports = noteCtrl;